home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 3 / BBS in a box - Trilogy III.iso / Files / Prog / B-C / C++ FAQ Reference 1.0 / C++ FAQ Reference 1.0.rsrc / TEXT_1132.txt < prev    next >
Encoding:
Text File  |  1993-06-30  |  505 b   |  10 lines

  1. Provide a friend operator<< that calls a protected virtual function:
  2.     class X {
  3.     protected:
  4.       virtual void print(ostream& o) const;  //or '=0;' if 'X' is abstract
  5.     public:
  6.       friend ostream& operator<<(ostream& o,const X& x) {x.print(o); return o;}
  7.       //...
  8.     };
  9.  
  10. Now all subclasses of X merely provide their own 'print(ostream&)const' member function, and they all share the common '<<' operator.  Friends don't bind dynamically, but this technique makes them *act* as if they were.